Welcome![Sign In][Sign Up]
Location:
Search - windows forms

Search list

[CSharpWindows聊天客户端

Description: 用C#做的Windows聊天客户端,有窗体设计等-done with C# Windows chat client, forms design
Platform: | Size: 16857 | Author: | Hits:

[Internet-Network用D3D模拟地月系

Description:

 

 

 

  一、建立空窗体

  新建一个工程,添加引用,并导入名称空间。

  加入一个设备对象变量:

private Microsoft.DirectX.Direct3D.Device device = null;

  添加初始化图形函数,并在这里面对设备对象进行实例化:

public void InitializeGraphics()
{
 PresentParameters presentParams = new PresentParameters();
 presentParams.Windowed = true;
 presentParams.SwapEffect = SwapEffect.Flip;
 presentParams.AutoDepthStencilFormat = DepthFormat.D16;
 presentParams.EnableAutoDepthStencil = true;
 device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this,  CreateFlags.HardwareVertexProcessing, presentParams);
}

  当程序执行时,需要绘制场景,代码在这个函数里:

public void Render()

{
 // 清空设备,并准备显示下一帧。
 device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black , 1.0f, 0);
 // 设置照相机的位置
 SetupCamera();
 //开始场景
 device.BeginScene();
 if(meshLoaded)
 {
  mesh.Render(meshLoc);
 }
 device.EndScene();
 //显示设备内容。
 device.Present();
}

  设置照相机的位置:

private void SetupCamera()
{
 device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 1000.00f);
 device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f ,0.0f, 20.0f), new Vector3(0.0f,0.0f, 0.0f), new Vector3(0,1,0));
}

  现在改变主函数,调用我们写的初始化函数,并显示场景:

[STAThread]

static void Main()
{
 using (Form1 EarthForm = new Form1())
 {
  EarthForm.InitializeGraphics();
  EarthForm.Show();

  while(EarthForm.Created)
  {
   EarthForm.Render();
   Application.DoEvents();
  }
  EarthForm.Dispose();
}

  运行程序,会显示一个空的窗体。

  二、加入地球:

  在这一步里需创建一个3D网格对象,来作为要显示的地球,为此,在工程中新加入一个类Earth,此类可以包含所创建的网格对象的信息。

  加入一些相关变量,含义见注释:

public class Earth : BaseEarth
{
 private Material[] mMaterials; //保存材质
 private Texture[] mTextures; //保存纹理
 private Matrix locationOffset; //用来保存网格对象的相对位置
 private Mesh mMesh = null; //三角形网格对象
 private Device meshDevice; //需要显示在哪个设备上。
}

  在构造函数中,把Device设备拷贝到私有成员变量,这样就可以在这个类的其它方法内使用它,另外就是把位置变量进行赋值:

public Earth(ref Device device, Matrix location): base(ref device)
{
 meshDevice = device;
 locationOffset = location;
}

  下面这个函数是装入.X文件。

public bool LoadMesh(string meshfile)
{
 ExtendedMaterial[] mtrl;
 try
 {
  // 装载文件
  mMesh = Mesh.FromFile(meshfile, MeshFlags.Managed, meshDevice, out mtrl);
  // 如果有材质的话,装入它们
  if ((mtrl != null) && (mtrl.Length > 0))
  {
   mMaterials = new Material[mtrl.Length];
   mTextures = new Texture[mtrl.Length];

   // 得到材质和纹理

   for (int i = 0; i < mtrl.Length; i++)
   {
    mMaterials[i] = mtrl[i].Material3D;
    if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))

 

    {
     //前面得到的纹理的路径是相对路径,需要保存的是绝对路径,通过应用程序路径可以获得
     mTextures[i] = TextureLoader.FromFile(meshDevice, @"..\..\" + mtrl[i].TextureFilename);
    }
   }
  }
  return true;
 }
 catch
 {
  return false;
 }
}

  在这个方法内,使用Mesh.FromFile()这个方法,从给定的文件名中找到.X文件,并装入相关数据,一旦数据格式设置完成,可以从此文件中找到材质和贴图信息,并把它存放在数组中,并通过文件路径,得到纹理文件文件的路径,最后返回真值,如果整个过程出现错误,返回假值。

  下面这个Render()方法,是把此对象,即地球显示在设备对象上,此方法较简单,通过变形操作来得到网格对象的X,Y,Z坐标,接着设置网格对象的材质和纹理,最后,将每个材质和纹理应用到每个网格。

public void Render(Matrix worldTransform)
{
 /把位置变为世界坐标
 meshDevice.Transform.World = Matrix.Multiply(locationOffset, worldTransform);
 //绘制网格
 for (int i = 0; i < mMaterials.Length; i++)
 {
  meshDevice.Material = mMaterials[i];
  meshDevice.SetTexture(0, mTextures[i]);
  mMesh.DrawSubset(i);
 }
}

  现在回到窗体代码中,添加引用网格对象的相关变量:

private Earth mesh = null;
private Matrix meshLoc;
private bool meshLoaded = false;

  在图形初始化函数中,需要对网格对象进行初始化。加入下面的代码:

meshLoc = Matrix.Identity;
meshLoc.M41 = 2.0f;
mesh = new Earth(ref device, meshLoc);
if (mesh.LoadMesh(@"..\..\earth.x"))
{
 meshLoaded = true;
}

  代码第一句把网格对象的位置定为原点,接着偏移X轴2个单位,接下来从文件中得到此.X文件。如果成功设置,meshLoaded置为真。注意,这里有一个.X文件,在源代码中有此文件。

 


  在设置相机的函数中,加入一盏灯光:

device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Direction = new Vector3(0, -1, -1);
device.Lights[0].Update();
device.Lights[0].Enabled = true;


  此灯光较简单,仅为一个直射型白光灯。

最后,在Render()方法中,调用网格对象的Render()方法,以显示地球。

 

  三、使地球旋转

  前面用一个网格对象来建立地球,但此类没有平移,旋转及缩放等方法,下面就加入这些方法,因为这些方法具有通用性,因此可以新建一个类,把这些方法写在这些类中,使地球对象成为它的派生类。

  在工程中新添加一个类:BaseEarth;

  加入进行平移、旋转、缩放的变量:

 

private float xloc = 0.0f;
private float yloc = 0.0f;
private float zloc = 0.0f;
private float xrot = 0.0f;
private float yrot = 0.0f;
private float zrot = 0.0f;
private float xscale = 1.0f;
private float yscale = 1.0f;
private float zscale = 1.0f;


  加入相应的属性代码:

 

public float XLoc
{
 get
 {
  return xloc;
 }
 set
 {
  xloc = value;
 }
}
…………

 

  在Render()虚函数中,应用平移、旋转及缩放。
 

public virtual void Render()
{
 objdevice.MultiplyTransform(TransformType.World,Matrix.Translation(xloc, yloc, zloc));
 objdevice.MultiplyTransform(TransformType.World,Matrix.RotationAxis(new Vector3(1.0f, 0.0f, 0.0f), xrot));
 objdevice.MultiplyTransform(TransformType.World,Matrix.RotationAxis(new Vector3(0.0f, 1.0f, 0.0f), yrot));
 objdevice.MultiplyTransform(TransformType.World,Matrix.RotationAxis(new Vector3(0.0f, 0.0f, 1.0f), zrot));
 objdevice.MultiplyTransform(TransformType.World,Matrix.Scaling(xscale, yscale, zscale));
 return;
}

 

  现在回到地球类,需要将其改为新类的派生类,同时更改构造函数,另外,在Render()方法中,应先调用基类的Render()方法:


public override void Render()
{
 base.Render();
 //把位置变为世界坐标
 // meshDevice.Transform.World = Matrix.Multiply(locationOffset, worldTransform);
 //绘制网格
 。。。。。。
}

 


  现在,由于在基类中可以设置对象位置,因此,可以把与locationOffset相关,即与设置位置的变量及语句注释掉。

  四、加入月球

  在这一步加入月球,实际上是再创建一个网格对象新实例,只是把纹理进行更改即可,为了代码模块性更好,把两个对象放在一个新类CModel中,在工程中新添加一个类CModel,并声明对象实例。


public class cModel
{
 private cMeshObject mesh1 = null;
 private cMeshObject mesh2 = null;
 private bool modelloaded;
}


  把窗口代码中的Load()事件,放在CModel中,这次不仅生成了地球,而且生成了月球。

 


public void Load(ref Device device)
{
 mesh1 = new Earth(ref device);
 mesh2 = new Earth(ref device);
 if (mesh1.LoadMesh(@"..\..\earth2.x"))
 {
  modelloaded = true;
 }
 else
 {
  modelloaded = false;
 }
 if (mesh2.LoadMesh(@"..\..\moon.x"))
 {
  mesh2.XLoc += 20.0f;
  modelloaded = true;
 }
 else
 {
  modelloaded = false;
 }
}

 

  下面的Update()方法中,参数dir 用来判断是顺时针旋转还是逆时针旋转,另外,地球和月球绕Y轴增加的角度大小不同,也就决定了二者旋转的速度不同。


public void Update(int dir)
{
 if(dir > 0)
 {
  mesh1.YRot += 0.02f;
  mesh2.YRot += 0.05f;
 }
 else if(dir < 0)
 {
  mesh1.YRot -= 0.02f;
  mesh2.YRot -= 0.05f;
 }
}


  在下面的render()方法中,生成显示月球和地球:

 


public void Render(ref Device device)
{
 device.Transform.World = Matrix.Identity;
 if(modelloaded)
 {
  mesh1.Render();
  mesh2.Render();
 }
}


  把窗口代码中的加入灯光的方法,也放在此类中:


public void LoadLights(ref Device device)
{
 device.Lights[0].Type = LightType.Directional;
 device.Lights[0].Diffuse = Color.White;
 device.Lights[0].Position = new Vector3(0.0f, 0.0f, 25.0f);
 device.Lights[0].Direction = new Vector3(0, 0, -1);
}
public void Light(ref Device device)
{
 device.Lights[0].Update();
 device.Lights[0].Enabled = true;
}


  五、与鼠标交互操作

  为了实现与键盘、鼠标交互,新添加一个类:CMouse,添加引用Microsoft.DirectX.DirectInput,并添加命名空间。加入相关变量:


private Microsoft.DirectX.DirectInput.Device mouse = null;
public System.Threading.AutoResetEvent MouseUpdated;
private float x, y, z = 0.0f;
private byte[] buttons;

 

  在下面的构造函数代码中,首先创建鼠标设备,并初始化回调事件:


public CMouse(System.Windows.Forms.Control control)
{
 mouse = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);
 mouse.SetCooperativeLevel(control, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
 mouse.Properties.AxisModeAbsolute = false;
 MouseUpdated = new System.Threading.AutoResetEvent(false);
 mouse.SetEventNotification(MouseUpdated);
 mouse.Acquire();
 Update();

 


  下面的Update()方法中获得鼠标的坐标值,并赋给私有成员变量:

public void Update()
{
 MouseState state = mouse.CurrentMouseState;
 x = state.X;
 y = state.Y;
 z = state.Z;
 buttons = state.GetMouseButtons();
}


  还需要有一个函数来检测鼠标左键是否按下:

 


public bool LeftButtonDown
{
 get
 {
  bool a;
  return a = (buttons[0] != 0);
 }
}


  六、大结局

  现在已经做完了准备工作,返回到窗口代码中,需要对这里的代码重新进行一些调整:

  在图形初始化函数中创建一个CModel类及CMouse类:

 

private CModel model = null;
private CMouse mouse = null;
private bool leftbuttondown = false;
private float mousexloc;

 

  添加对鼠标初始化的方法:

 

网管联盟bitsCN@com


public void InitializeInput()
{
 mouse = new CMouse(this);
}


  添加UpdateInputState()方法,当按下鼠标左键时,将leftbuttondown值设置为真,当鼠标抬起时,将mousexloc置0:


private void UpdateInputState()
{
 mouse.Update();
 if (mouse.LeftButtonDown)
 {
  if(leftbuttondown == false)
  {
   mousexloc = 0.0f;
   leftbuttondown = true;
  }
  else
  {
   mousexloc = -mouse.X;
  }
 }
 else
 {
  leftbuttondown = false;
  mousexloc = 0.0f;
 }
}


  在此程序中,只对X值进行了操作,即只能左右转。

  Render()方法更新如下:

public void Render()
{
 UpdateInputState();
 device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkGray, 1.0f, 0);
 SetupCamera();
 device.BeginScene();
 model.Update((int)mousexloc);
 model.Light(ref device);
 model.Render(ref device);
 device.EndScene();
 device.Present();
}

 

  最后更改Main()主函数:


static void Main()
{
 using (Form1 EarthForm = new Form1())
 {
  EarthForm.InitializeGraphics();
  EarthForm.InitializeInput();
  EarthForm.Show();
  while(EarthForm.Created)
  {
   EarthForm.Render();
   Application.DoEvents();
  }
  EarthForm.Dispose();
 }
 


Platform: | Size: 11817 | Author: mantoutou | Hits:

[ASPBeginning ASP.NET 3.5 in C# 2008 From Novice to Professional, Second Edition

Description: Editorial Reviews Product Description The most up–to–date and comprehensive introductory ASP.NET book you’ll find on any shelf, Beginning ASP.NET 3.5 in C# 2008 guides you through Microsoft’s latest technology for building dynamic web sites. This book will enable you to build dynamic web pages on the fly, and it assumes only the most basic knowledge of C#. The book provides exhaustive coverage of ASP.NET, guiding you from your first steps right up to the most advanced techniques, such as querying databases from within a web page and tuning your site for optimal performance. Within these pages, you’ll find tips for “best practices” and comprehensive discussions of key database and XML principles you need to know in order to be effective with ASP.NET. The book also emphasizes the invaluable coding techniques of object orientation and code behind, which will start you off on the track to building real–world web sites right from the beginning—rather than just faking it with simplified coding practices. By the time you’ve finished the book, you will have mastered the core techniques and have all the knowledge you need to begin work as a professional ASP.NET developer. About the Author Matthew MacDonald is an author, educator, and MCSD developer who has a passion for emerging technologies. He is a regular writer for developer journals such as Inside Visual Basic, ASPToday, and Hardcore Visual Studio .NET, and he's the author of several books about programming with .NET, including User Interfaces in VB .NET: Windows Forms and Custom Controls, The Book of VB .NET, and .NET Distributed Applications. In a dimly remembered past life, he studied English literature and theoretical physics. Send e-mail to him with praise, condemnation, and everything in between, to p2p@prosetech.com.
Platform: | Size: 8946746 | Author: open008 | Hits:

[OtherVB.NET - Windows Forms - How-To Automate Office.ra

Description: Tutorial on ms office automation with vb .net
Platform: | Size: 721920 | Author: er | Hits:

[Multimedia Developmci2

Description: 一个我做的播放器,可以调用Windows MediaPlayer的解码器进行播放,支持播放列表,同时可以保存播放列表和加载。窗体可以折叠。-I do a player that can call the Windows Media Player for player decoder, support playlists, while preserving playlists and loading. Forms can be folded.
Platform: | Size: 342016 | Author: 刘晓明 | Hits:

[JSP/JavaResume

Description: 个人简历的小程序.Windows形式.简单,初学者易明.-biographical notes on the small program. Windows forms. Simple, easy to understand beginners.
Platform: | Size: 1024 | Author: 梁家辉 | Hits:

[GUI DevelopTCWIN

Description: 窗口开发 TC 源码,简单的指令形式-window TC-source development, the simple directive forms
Platform: | Size: 4096 | Author: 欧阳凡 | Hits:

[GUI Developchgskinprj

Description: 看一个软件是否优秀,除了功能健全、性能稳定、容易操作之外,软件界面的美观越来越受到人们的关注。人们不满足于传统的矩形windows窗体,不规则窗体的软件到处可见,更进一步发展到软件界面由用户根据自己的爱好自由控制,真正实现了软件对用户的友好性。而要实现这一功能,软件换肤技术就是基础。例子代码介绍一种简单明了的方法来制作一个登陆对话框,本程序编程环境C++Builder。 [代码性质] BCB完整应用程序代码 [代码作者] 吴康彬 [文件大小] 667K-see an excellent software, in addition to a well-functioning, stable performance and ease of use, the software interface to be more attractive to the people's concern. People unsatisfied with the traditional rectangular windows forms, irregular forms can be seen in the software, further development of the software by the user interface according to their own freedom-loving control, the actual realization of the right software user-friendly. To achieve this functionality, the software technology is the basis Eurocargo. Example code presents a simple method to produce a dialog landing, the C programming environment Builder. [Code nature] BCB complete application code [code writers] Tunkangban [File Size : 667K
Platform: | Size: 667648 | Author: 张永新 | Hits:

[xml-soap-webserviceWindowsXMLWebservices

Description: 如何通过使用 .NET 框架的内置异步支持,从 Visual Basic .NET Windows 窗体客户端应用程序调用 XML Web services。该应用程序 XML Web services 采用一个输入参数??在返回到客户端之前延迟的毫秒数??并将一个随机数返回到调用方。此 Windows 窗体客户端应用程序允许用户指定延迟的毫秒数,并对 XML Web services 的同步和异步调用。-how to use.NET framework with built-in support for asynchronous, from Visual Basic.NET Windows Forms client application called XML Web services. The application of XML Web services using an input parameter for the return to the client before the millisecond delay will be a few random numbers return to the calling party. This Windows Forms client application allows users to designate a few milliseconds of delay, and XML Web services synchronous and asynchronous calls.
Platform: | Size: 128000 | Author: | Hits:

[GUI DevelopVB.NET - Windows Forms - XP Theme Support

Description: 微软的经典代码,希望对学习VB.NET的有所帮助-the classic Microsoft code, and I hope to learn VB.NET help
Platform: | Size: 124928 | Author: 张莉蔓 | Hits:

[OS programVB.NET - Windows Forms - Use the Clipboard

Description: 还可以的代码,希望可以啊-code can also hope ah
Platform: | Size: 162816 | Author: 张莉蔓 | Hits:

[CSharpWindows聊天客户端

Description: 用C#做的Windows聊天客户端,有窗体设计等-done with C# Windows chat client, forms design
Platform: | Size: 39936 | Author: | Hits:

[CSharpoocr_v2_07252004

Description: sdfa Open OCR (oocr) is a character recognition program used for automatic scanning of documents and forms. oocr is based on the idea of automatic form/document reading, which can create a batch process in recognizing areas of interest for forms or automatically scan entire documents and scanned images for character recognition. oocr is written in C on Windows (win32) libraries. All of the source code to the program is made available under the GNU General Public License (GPL) _________________________________________________________ History July 25,2004 : 2nd Release [NOT to be intended as a stable working release] Added: Program Icon Started: Image Processing for background detection Started: Bound Box reduction/fitting for standard scanning July 16,2004 : Initial Release [NOT to be intended as a stable working release]
Platform: | Size: 121856 | Author: 苏打发 | Hits:

[Windows DevelopFrontPage2002

Description: FrontPage2002网页制作.运用FrontPage 2002, 我们可以轻松的在Web页中插入文字、图片、表格、表单;可以在FrontPage 2002环境下,直接查看Web页的HTML代码,并能根据需要修改HTML代码;可以插入DHTML组件及各种脚本语言,使网页具有动态效果;还可以在Web页中添加各种多媒体元素,如动画、声音甚至视频,从而创建出图文并茂、生动活泼的、交互性很强的Web页面。-FrontPage2002 pages. Using FrontPage 2002, we can easily to the Web page, insert text, pictures, forms, the form; The FrontPage 2002 environment, a direct view of the Web page's HTML code, and needed to modify the HTML code; DHTML can be inserted into various components and feet the language that the website has a dynamic effect; also in the Web page can add multimedia elements, such as animations, sound and even video, thus creating illustrations and lively, highly interactive Web pages.
Platform: | Size: 392192 | Author: z1 | Hits:

[Menu controlVisual CSharp的菜单编程

Description: 本文主要是介绍了在用Visual C#进行有关菜单编程中,可能遇到有些问题。在解决这些问题的时候主要用到的是名称空间System.Windows.Forms中定义的二个对象MainMenu、MenuItem和这二个对象中定义的一些方法。当然这些方法还有一些其他的用法,有兴趣的读者可以参考有关书籍和浏览MSDN网站。-this paper is introduced in Visual C# programming on the menu, they may encounter some problems. In addressing these issues when the major use of the name space is defined in the System.Windows.Forms two MainMenu object, and that two MenuItem object definition of the method. Of course there are other methods to use, interested reader can refer to the books and visit the MSDN website.
Platform: | Size: 31744 | Author: 开远 | Hits:

[Otherjava程序集合1

Description: JAVA程序集合1 包括窗体 文体 WINDOWS命令 对话框 画图-Java procedures, including a set of Windows Forms Style dialog box drawing
Platform: | Size: 373760 | Author: 何锡友 | Hits:

[GUI Developalter-windows

Description: dephi编程,可变窗体,当窗体大小变化,其内容也变化-dephi programming, variable forms, Form size change, its contents change
Platform: | Size: 438272 | Author: 文豪 | Hits:

[Dialog_Windowboom-windows

Description: dephi编程,爆炸窗体,并能发出爆炸的声音,处理不规则的透明窗体可以实现(下一个文件将会上传透明窗体源码)-dephi programming, Form explosion, and an explosion of sound, transparent handling of irregular forms can be achieved (a transparent paper forms will be uploaded source)
Platform: | Size: 186368 | Author: 文豪 | Hits:

[GUI DevelopDockMgr1_2_1

Description: DockManager control is designed to achieve docking capability for MDI forms. It can be used to develop applications with the same look and feel as Visual Studio .Net DockManager control is a 100%-native .Net windows forms control, written in C# -DockManager control is designed to achieve docking capability for MDI forms. It can be used to develop applications with the same look and feel as Visual Studio. Net DockManager control is a 100%- native. Net windows forms control, written in C#
Platform: | Size: 373760 | Author: 王二 | Hits:

[Windows Developmanagedspy

Description: ManagedSpy是一个诊断工具,和Spy++类似。它显示受托管的属性,允许你记录事件,并且是一个使用ManagedSpyLib的很好例子。ManagedSpyLib引入了一个称为ControlProxy的类。一个ControlProxy是一个System.Windows.Forms.Control的代表,允许你获得或设置属性并预定事件好似你正在目标进程中运行着一样。使用ManagedSpyLib来做自动化测试,为兼容性做事件记录进程交互通讯,或白盒测试。 -ManagedSpy is a diagnostic tool, and Spy similar. It shows hosted by the attributes allow you to record the incident, and is a very good use of examples ManagedSpyLib. ManagedSpyLib introduction of a category known as the ControlProxy. One is a ControlProxy System.Windows.Forms.Control representatives, or to allow you to set properties and scheduled events like your goal is the process of running the same. ManagedSpyLib make use of automated testing for compatibility process so recorded interactive communications, or White Box.
Platform: | Size: 423936 | Author: 杰子 | Hits:
« 1 2 3 4 56 7 8 9 10 ... 30 »

CodeBus www.codebus.net